package view.admin.userManage;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ToggleGroup;
import pojo.user.UserEntity;
import view.Main;
import controller.UserController;

import java.io.IOException;

/**
 * @author Limohan
 */
public class SetNewUserController {
    private Main myApp;

    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public SetNewUserController() {
    }
    @FXML
    private JFXRadioButton jingXiao;

    @FXML
    private ToggleGroup group;

    @FXML
    private JFXRadioButton yun;

    @FXML
    private JFXTextField newAccount;

    @FXML
    private JFXTextField newPassword;

    @FXML
    private JFXTextField newName;

    @FXML
    private JFXTextField newPhone;

    @FXML
    private JFXTextField newFactoryName;

    @FXML
    private JFXTextField newDetail;

    @FXML
    private JFXButton newUser;

    @FXML
    private JFXButton returnUserManage;

    @FXML
    void confirmNewUser(ActionEvent event) throws IOException {
        UserEntity userEntity = new UserEntity();
        UserController userController = UserController.getInstance();
        userEntity.setAccount(newAccount.getText());
        userEntity.setPassword(newPassword.getText());
        userEntity.setName(newName.getText());
        userEntity.setPhone(newPhone.getText());
        jingXiao.setUserData("经销商");
        yun.setUserData("云工厂");
        //判断power类型
        if(group.getSelectedToggle()==null) {
            userEntity.setPower(null);
        }else{
            userEntity.setPower(group.getSelectedToggle().getUserData().toString());
        }

        userEntity.setDetail(newDetail.getText());
        userEntity.setFactoryName(newFactoryName.getText());
        userEntity.setState("关停");
        String flag = userController.register(userEntity);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(flag);
        alert.showAndWait();

    }

    @FXML
    void returnUserManage(ActionEvent event) throws Exception {
        myApp.goToAdminUserControllerPage();
    }
}
